package com.sp.scmdb.action;

import java.util.HashMap;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.MapBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.ConfLabelIOUpload;
import com.sp.scmdb.webservice.service.UploadLabelIOService;
import com.sp.scmdb.webservice.serviceImpl.UploadLabelIOServiceImpl;

@Controller("uploadLabelIOAction")
@Scope("prototype")
public class UploadLabelIOAction extends BaseAction<MapBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(UploadLabelIOAction.class);
	
	private UploadLabelIOService uploadLabelIOService;
	private String autoUploadTime;
	private String uploadStatus;
	private String lastUploadTime;
	private String result;
	private String messageUploadLabelIO;

	public String init() {
		try{
			ConfLabelIOUpload conf = uploadLabelIOService.getUsedConfLabelIOUpload();
			if (conf != null) {
				autoUploadTime = conf.getAutoUploadTime().toString();
				uploadStatus = conf.getUploadStatus().toString();
				lastUploadTime = conf.getLastUploadTime().toString("yyyy-MM-dd HH:mm:ss.SSS");
				messageUploadLabelIO = UploadLabelIOServiceImpl.MESSAGE_LABELIO_UPLOAD;
			}			 
		}catch(Exception e){
			log.error("findAll fail!reason:" + e.getMessage());
		}
		return "init";
	}
	
	public String uploadLabelIO(){
		String message = uploadLabelIOService.uploadLabelIO(autoUploadTime);
		if ("10".equals(message)){
			messageSession("0", "操作失败！");
		} else {
			ConfLabelIOUpload conf = uploadLabelIOService.getUsedConfLabelIOUpload();
			if (conf != null) {
				autoUploadTime = conf.getAutoUploadTime().toString();
				uploadStatus = conf.getUploadStatus().toString();
			}
			messageSession(CommonParamter.CHECK_SUCCESS, "");
		}
		
		return "upload";
	}
	
	public String stopUploadLabelIO(){
		uploadLabelIOService.stopUploadLabelIO();
		ConfLabelIOUpload conf = uploadLabelIOService.getUsedConfLabelIOUpload();
		if (conf != null) {
			autoUploadTime = conf.getAutoUploadTime().toString();
			uploadStatus = conf.getUploadStatus().toString();
		}
		messageSession(CommonParamter.CHECK_SUCCESS, "");
		return "upload";
	}
	
	public String getRealtimeConf(){
		
		try {
			ConfLabelIOUpload conf = uploadLabelIOService.getUsedConfLabelIOUpload();
			String lastTime = "";
			if (conf.getLastUploadTime() != null) {
				lastTime = conf.getLastUploadTime().toString("yyyy-MM-dd HH:mm:ss.SSS");
			}
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("lastUploadTime", lastTime);
			map.put("uploadStatus", conf.getUploadStatus().toString());
			// 将entity转换成json
			JSONArray jo = JSONArray.fromObject(map);
			result = jo.toString();
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
		}
		
		return SUCCESS;
	}

	public UploadLabelIOService getUploadLabelIOService() {
		return uploadLabelIOService;
	}

	public void setUploadLabelIOService(UploadLabelIOService uploadLabelIOService) {
		this.uploadLabelIOService = uploadLabelIOService;
	}

	public String getAutoUploadTime() {
		return autoUploadTime;
	}

	public void setAutoUploadTime(String autoUploadTime) {
		this.autoUploadTime = autoUploadTime;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUploadStatus() {
		return uploadStatus;
	}

	public void setUploadStatus(String uploadStatus) {
		this.uploadStatus = uploadStatus;
	}

	public String getLastUploadTime() {
		return lastUploadTime;
	}

	public void setLastUploadTime(String lastUploadTime) {
		this.lastUploadTime = lastUploadTime;
	}

	public String getMessageUploadLabelIO() {
		return messageUploadLabelIO;
	}

	public void setMessageUploadLabelIO(String messageUploadLabelIO) {
		this.messageUploadLabelIO = messageUploadLabelIO;
	}
	
}
